package cc.Utils;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @Auther: ${张智杰}
 * @Date: 2018/10/11 11:56
 * @Description:
 */
public class PictureUtil {
    /**
     * 上传文件
     * @param file
     * @param request
     * @return
     */
    public static String uploadFile(MultipartFile file, HttpServletRequest request) throws IOException {
        String path = request.getSession().getServletContext().getRealPath("/img");
        //为了避免文件名重复，使用UUID
        String fileName = UUIDUtil.getUUID32()+"-"+file.getOriginalFilename();
        File dir = new File(path, fileName);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        file.transferTo(dir);
        //上传成功后返回
        return  "/img/"+fileName;

    }

}
